package com.nmsl.statistic.controller;

import com.github.pagehelper.Page;
import com.nmsl.common.core.controller.BaseController;
import com.nmsl.common.core.domain.AjaxResult;
import com.nmsl.common.core.page.TableDataInfo;
import com.nmsl.statistic.domain.MedicInputDetailStro;
import com.nmsl.statistic.mapper.RetailMedicineMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/medicstorastats")
public class MedicineStoragestatsController extends BaseController {
    @Autowired
    RetailMedicineMapper retailMedicineMapper;
    @RequestMapping("/inputmed")
    public TableDataInfo inputMedicDetail(MedicInputDetailStro medicInputDetailStro)   {
//        medicInputDetail
        startPage();
        Map<String, Object> params = medicInputDetailStro.getParams();
        System.out.println(params);
        List<MedicInputDetailStro> list = retailMedicineMapper.medicInputDetail(medicInputDetailStro);

        List<MedicInputDetailStro> collect = list.stream().map(item -> {
            item.setAmountpurchase(item.getPurchasePrice().multiply(new BigDecimal(item.getStorageInQuantity().toString())));
            return item;
        }).collect(Collectors.toList());
        BigDecimal amountpurchase = new BigDecimal(0);
        for (MedicInputDetailStro inputDetailStro : collect) {
            amountpurchase =  amountpurchase.add(inputDetailStro.getAmountpurchase());
        }
        MedicInputDetailStro medicInputDetailStro1 = new MedicInputDetailStro();
        medicInputDetailStro1.setAmountpurchase(amountpurchase);
        medicInputDetailStro1.setMedicineName("合计");
        collect.add(0,medicInputDetailStro1);
        System.out.println(collect);
        return getDataTable(collect);

    }

    @RequestMapping("/outputmed")
    public TableDataInfo outputMedicDetail(MedicInputDetailStro medicInputDetailStro)   {
//        medicInputDetail
        startPage();
        List<MedicInputDetailStro> list = retailMedicineMapper.medicoutputDetail(medicInputDetailStro);

        List<MedicInputDetailStro> collect = list.stream().map(item -> {
            item.setAmountpurchase(item.getPurchasePrice().multiply(new BigDecimal(item.getStorageInQuantity().toString())));
            return item;
        }).collect(Collectors.toList());
        BigDecimal amountpurchase = new BigDecimal(0);
        for (MedicInputDetailStro inputDetailStro : collect) {
            amountpurchase =  amountpurchase.add(inputDetailStro.getAmountpurchase());
        }
        MedicInputDetailStro medicInputDetailStro1 = new MedicInputDetailStro();
        medicInputDetailStro1.setAmountpurchase(amountpurchase);
        medicInputDetailStro1.setMedicineName("合计");
        collect.add(0,medicInputDetailStro1);
        System.out.println(collect);
        return getDataTable(collect);

    }
}
